﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GrainManage
{
    public class Log
    {
        public static void Write(string error, Exception e)
        {
            try
            {
                string dicrectory = AppSettings.Get("LogPath");
                if (!string.IsNullOrEmpty(dicrectory) && Path.IsPathRooted(dicrectory) && Directory.Exists(dicrectory))
                {
                    string path = Path.Combine(dicrectory, "grainmanage@client.log");
                    StringBuilder builder = new StringBuilder();
                    builder.AppendLine("******************************" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "******************************");
                    if (!string.IsNullOrEmpty(error))
                    {
                        builder.AppendLine("Error: " + error);
                    }
                    if (!string.IsNullOrEmpty(e.Message))
                    {
                        builder.AppendLine("Message: " + e.Message);
                    }
                    if (!string.IsNullOrEmpty(e.Source))
                    {
                        builder.AppendLine("Source: " + e.Source);
                    }
                    if (e.TargetSite != null)
                    {
                        builder.AppendLine("TargetSite: " + e.TargetSite.DeclaringType.FullName + "." + e.TargetSite.Name);
                    }

                    if (!string.IsNullOrEmpty(e.StackTrace))
                    {
                        builder.AppendLine("StackTrace: " + e.StackTrace);
                        builder.AppendLine("StackTrace: ");
                    }

                    builder.AppendLine();
                    File.AppendAllText(path, builder.ToString());
                }
            }
            catch (Exception)
            {

            }
        }
    }
}
